#include "config.h"
#include "stm32f10x.h"
#include "bsp_usart1.h"
#include "bsp_hcsr04.h"
#include "bsp_systick.h"
#include "bsp_led.h"
#include "system.h"
#include "bsp_dht11.h"
#include "bsp_ds18b20.h"
#include "bsp_oled.h"
#include "bsp_adc.h"


#include <rthw.h>  		
#include <rtthread.h> 

// uint8_t ucDs18b20Id[8] = {0};
// DS18B20_ReadId(ucDs18b20Id);
// temperature = DS18B20_GetTemp_MatchRom(ucDs18b20Id);

void thread01_entry(void *parameter)
{
	uint32_t temperature = 0;
	uint32_t humidity = 0;
	uint16_t value = 0;
	char str[17] = {0};
	int32_t Distance = 0;
	while(1)
	{
		Get_temperature_humidity(&temperature, &humidity);
		sprintf(str, "T:%dC    \0", temperature);
        // LOG("DHT11: temperature is %u, humidity is %u", temperature, humidity);
		OLED_ShowString(1, 1, str);
		sprintf(str, "H:%d%%   \0", humidity);
		OLED_ShowString(1, 8, str);

		sprintf(str, "Distance:%dmm \0", Distance = HC_SR04_GetDistance_mm());
		OLED_ShowString(2, 1, str);

		sprintf(str, "AD_Value:%d  \0", value = AD_GetValue());
		OLED_ShowString(3, 1, str);
		
		sprintf(str, "Tick:%llu\0", G_Timer_Tick);
		OLED_ShowString(4, 1, str);

//		if ((Distance > 2000) || value < 1000) Beep_Control(1);
//		else Beep_Control(0);
        rt_thread_mdelay(100); // 延时 1000 毫秒
	}
}

void thread02_entry(void *parameter)
{
	while(1)
	{
		LED_D4_TOGGLE();
        rt_thread_mdelay(500); // 延时 500 毫秒
	}
}

void thread03_entry(void *parameter)
{
	// OLED_ShowString(1, 1, "Test01");
//	uint16_t value = 0;
	while(1)
	{
		// value = AD_GetValue();
		// LOG("The ADC value is %d", value);
        rt_thread_mdelay(1000); // 延时 500 毫秒
	}
}



int main(void)
{

	HC_SR04_Init();
	DS18B20_Init();
	OLED_Init();
	AD_Init();
	LOG("Hello world ! \r\n");
	

	rt_thread_t thread01 = RT_NULL;
	rt_thread_t thread02 = RT_NULL;
	rt_thread_t thread03 = RT_NULL;
//	rt_thread_t thread04 = RT_NULL;
	
	thread01 = rt_thread_create("thread01", thread01_entry, RT_NULL, 2048, 5, 10);
	thread02 = rt_thread_create("thread02", thread02_entry, RT_NULL, 256, 5, 10);
	thread03 = rt_thread_create("thread03", thread03_entry, RT_NULL, 1024, 5, 10);


	rt_thread_startup(thread01);
	rt_thread_startup(thread02);
	rt_thread_startup(thread03);
	
	while (1)
	{
		rt_thread_mdelay(1000);
		// int32_t Distance = HC_SR04_GetDistance_mm();
		// float temperature = DS18B20_GetTemp_SkipRom();
		// LOG("Distsnce is %d mm", Distance);
		// LOG("DS18B20: temperature is %lf degrees Celsius", temperature);
		// LOG("G_Timer_Tick is %llu", G_Timer_Tick);
	}
}

